package cn.crism.chatme.service.impl;

import cn.crism.chatme.enums.SearchFriendsStatusEnum;
import cn.crism.chatme.mapper.FriendRelationMapper;
import cn.crism.chatme.mapper.FriendsReqMapper;
import cn.crism.chatme.mapper.UserInfoMapper;
import cn.crism.chatme.pojo.FriendRelation;
import cn.crism.chatme.pojo.FriendsReq;
import cn.crism.chatme.pojo.UserInfo;
import cn.crism.chatme.service.UserService;
import cn.crism.chatme.utils.QRCodeUtils;
import cn.crism.chatme.utils.QiniuCloudUtil;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @CreateDate: 2018/12/25 9:26 AM
 * @Description:
 * @Author: Crism
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private FriendRelationMapper friendRelationMapper;

    @Autowired
    private FriendsReqMapper friendsReqMapper;

    @Autowired
    private Sid sid;

    @Autowired
    private QRCodeUtils qrCodeUtils;

//    @Autowired
//    private FastDFSClient fastDFSClient;

    @Override
    public boolean userNameIsExist(String userName) {
        UserInfo user = new UserInfo();
        user.setUserName(userName);
        UserInfo result = userInfoMapper.selectOne(user);
        return result != null ? true : false;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserInfo userInfoIsExist(String username, String password) {

        Example userExample = new Example(UserInfo.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("userName", username);
        criteria.andEqualTo("password", password);

        UserInfo result = userInfoMapper.selectOneByExample(userExample);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public UserInfo saveUser (UserInfo user) {

        String userId = sid.nextShort();
        // 为每个用户生成一个唯一的二维码,二维码的内容其实就是一串字符
        String qrCodePath = "/root/chatMe/" + userId + "qrcode.png";
        qrCodeUtils.createQRCode(qrCodePath, "wechat_qrcode:" + user.getUserName() + "");
//        MultipartFile qrCodeImg = FileUtils.fileToMultipart(qrCodePath);
        String path = QiniuCloudUtil.uploadQNImg(qrCodePath, userId + "qrcode.png");
//        String qrCodeImgUrl = "";
//        try {
//            qrCodeImgUrl = fastDFSClient.uploadBase64(qrCodeImg);
//            FileUtils.deleteFile(qrCodePath);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }

        user.setQrCode(path);
        user.setUserId(userId);
        userInfoMapper.insert(user);
        return user;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public UserInfo updateUserInfo(UserInfo user) {
        userInfoMapper.updateByPrimaryKeySelective(user);
        return queryUserInfoByID(user.getUserId());
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserInfo queryUserInfoByID(String userId) {
        return userInfoMapper.selectByPrimaryKey(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Integer preconditionSearchFriend(String myUserId, String friendUserName) {

        /* 查询用户是否存在 */
        UserInfo userInfo = queryUserInfoByUserName(friendUserName);
        if (userInfo == null){
            return SearchFriendsStatusEnum.USER_NOT_EXIST.status;
        }
        /* 搜索的账号是自己 */
        if (userInfo.getUserId().equals(myUserId)){
            return SearchFriendsStatusEnum.IS_YOURSELF.status;
        }

        /* 已经是你的好友 */
        Example friendRelation = new Example(FriendRelation.class);
        Example.Criteria friendRelationCriteria = friendRelation.createCriteria();
        friendRelationCriteria.andEqualTo("ownerId", myUserId);
        friendRelationCriteria.andEqualTo("friendId",userInfo.getUserId());
        FriendRelation fr = friendRelationMapper.selectOneByExample(friendRelation);
        if (fr != null){
            return SearchFriendsStatusEnum.ALREADY_FRIENDS.status;
        }

        return SearchFriendsStatusEnum.SUCCESS.status;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserInfo queryUserInfoByUserName(String userName) {
        Example user = new Example(UserInfo.class);
        Example.Criteria userCriteria = user.createCriteria();
        userCriteria.andEqualTo("userName", userName);
        return userInfoMapper.selectOneByExample(user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void sendFriendRequest(String myUserId, String friendUserName) {
        UserInfo friendInfo = queryUserInfoByUserName(friendUserName);
        Example friendReq = new Example(FriendsReq.class);
        Example.Criteria friendReqCriteria = friendReq.createCriteria();
        friendReqCriteria.andEqualTo("sendUserId", myUserId);
        friendReqCriteria.andEqualTo("replyUserId", friendInfo.getUserId());
        if (friendsReqMapper.selectOneByExample(friendReq) == null){
            FriendsReq freq = new FriendsReq();
            freq.setReqId(sid.nextShort());
            freq.setSendUserId(myUserId);
            freq.setReplyUserId(friendInfo.getUserId());
            freq.setReqTime(new Date());
            friendsReqMapper.insert(freq);
        }
    }


}
